package org.example.studentsystem_back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.example.studentsystem_back.common.CommonResponse;
import org.example.studentsystem_back.domain.Freshmen;
import org.example.studentsystem_back.domain.Users;
import org.example.studentsystem_back.dto.PageDTO;
import org.example.studentsystem_back.mapper.UsersMapper;
import org.example.studentsystem_back.service.FreshmenService;
import org.example.studentsystem_back.mapper.FreshmenMapper;
import org.example.studentsystem_back.util.JwtUtil;
import org.example.studentsystem_back.vo.freshmenMangeVo.FreshmenVo;
import org.example.studentsystem_back.vo.userManageVo.PageFreshmanVo;
import org.example.studentsystem_back.vo.userManageVo.smallVo.PageFreshmanItem;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 朱嘉辉
* @description 针对表【freshmen】的数据库操作Service实现
* @createDate 2024-07-10 08:20:18
*/
@Service
public class FreshmenServiceImpl extends ServiceImpl<FreshmenMapper, Freshmen>
    implements FreshmenService{
    @Resource
    private FreshmenMapper freshmenMapper;
    @Resource
    private UsersMapper usersMapper;


    //获取所有新生信息
    @Override
    public CommonResponse<PageFreshmanVo> getAllFreshmen(PageDTO pageDto) {
        IPage<Freshmen> page = new Page<>(pageDto.getPage(),pageDto.getLimit());
        List<Freshmen> freshmenList =freshmenMapper.selectList(page,null);
        Long total = freshmenMapper.selectCount(null);

        List<PageFreshmanItem> itemList = new ArrayList<>();
        freshmenList.forEach(freshman -> {
            PageFreshmanItem pageFreshmanItem = new PageFreshmanItem();
            pageFreshmanItem.setDataByFreshman(freshman);
            pageFreshmanItem.setName(usersMapper.selectById(freshman.getUserid()).getName());
            itemList.add(pageFreshmanItem);
        });
        PageFreshmanVo pageFreshmanVo = new PageFreshmanVo();
        pageFreshmanVo.setPageList(itemList);
        pageFreshmanVo.setTotal(total);

        return CommonResponse.createForSuccess(pageFreshmanVo);
    }

    @Override
    public CommonResponse<FreshmenVo> getFreshmenByUserId(String userId) {
        QueryWrapper<Freshmen> freshmenQueryWrapper = new QueryWrapper<>();
        freshmenQueryWrapper.eq("userId",userId);
        Freshmen freshmen = freshmenMapper.selectOne(freshmenQueryWrapper);
        if(freshmen==null){
            return CommonResponse.createForSuccess("没有该userId的新生",null);
        }
        Users user = usersMapper.selectById(userId);
        FreshmenVo freshmenVo = freshmenToFreshVo(freshmen,user);

        return CommonResponse.createForSuccess("查询成功",freshmenVo);
    }

    @Override
    public CommonResponse<FreshmenVo> updateFreshmenUserId(String userId, Freshmen freshmen) {
        freshmenMapper.updateById(freshmen);
        Users user = usersMapper.selectById(userId);
        FreshmenVo freshmenVo = freshmenToFreshVo(freshmen,user);
        return CommonResponse.createForSuccess("更新成功",freshmenVo);
    }

    @Override
    public CommonResponse<FreshmenVo> deleteFreshmen(String userId) {
        QueryWrapper<Freshmen> freshmenQueryWrapper = new QueryWrapper<>();
        freshmenQueryWrapper.eq("userId",userId);
        Freshmen freshmen = freshmenMapper.selectOne(freshmenQueryWrapper);
        freshmenMapper.delete(freshmenQueryWrapper);
        Users user = usersMapper.selectById(userId);
        FreshmenVo freshmenVo = freshmenToFreshVo(freshmen,user);
        return CommonResponse.createForSuccess("删除成功",freshmenVo);
    }

    @Override
    public CommonResponse<FreshmenVo> getCurrentFreshmen(HttpServletRequest httpServletRequest) {
        String token = httpServletRequest.getHeader("token");
        JwtUtil.verifyToken(token);
        Users user = JwtUtil.getCurrentUser(token);
        QueryWrapper<Freshmen> queryWrapper = new QueryWrapper<>();
        if (user != null) {
            queryWrapper.eq("userid",user.getUserid());
        }
        Freshmen freshman = freshmenMapper.selectOne(queryWrapper);
        if(freshman==null)
        {
            return CommonResponse.createForSuccess("没有学生信息",null);
        }
        FreshmenVo freshmenVo = null;
        if (user != null) {
            freshmenVo = freshmenToFreshVo(freshman,user);
        }
        return CommonResponse.createForSuccess("查询成功",freshmenVo);
    }

    private FreshmenVo freshmenToFreshVo(Freshmen freshmen, Users user ){
        FreshmenVo freshmenVo = new FreshmenVo();
        freshmenVo.setFreshmanId(freshmen.getFreshmanId());
        freshmenVo.setUserid(freshmen.getUserid());
        freshmenVo.setMajor(freshmen.getMajor());
        freshmenVo.setEnrollmentDate(freshmen.getEnrollmentDate());
        freshmenVo.setSourceLocation(freshmen.getSourceLocation());
        freshmenVo.setPreviousSchool(freshmen.getPreviousSchool());
        freshmenVo.setPhone(freshmen.getPhone());
        freshmenVo.setEducation(freshmen.getEducation());
        freshmenVo.setHasCheckedIn(freshmen.getHasCheckedIn());
        freshmenVo.setPassword(user.getPassword());
        freshmenVo.setName(user.getName());
        freshmenVo.setGender(user.getGender());
        freshmenVo.setRole(user.getRole());

        return freshmenVo;
    }
}




